/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package azure;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import javax.swing.ImageIcon;
import javax.swing.JLabel;

/**
 * An extension of JLabel specifically for drawing manga pages, has an overridden paint() method to handle
 * resizing of the app window
 * @author Gargravarr
 * @date Aug 11, 2011
 */
public class ImageLabel extends JLabel
{

    public Image originalImage;
    public double scaleFactor;

    public ImageLabel(String title)
    {
	super(title);
    }

    public ImageLabel(ImageIcon icon)
    {
	super(icon);
    }

    public ImageLabel()
    {
	super();
    }

    @Override
    /**
     * Override the default Paint method to automatically scale the Image set in this object. Allows for dynamic scaling
     * when the label is resized within the form.
     * Credit Rob Moore of programmingcentre.com for
     * @parameter g The Graphics object
     */
    public void paintComponent(Graphics g)
    {
	if (originalImage != null)
	{
	    Graphics2D g2 = (Graphics2D)g;
//	    scaleFactor = getImageRatio();
//	    int newW = (int)(originalImage.getWidth(null) * scaleFactor);
//	    int newH = (int)(originalImage.getHeight(null) * scaleFactor);
	    int labelWidth = this.getWidth(),
		    labelHeight = this.getHeight();
	    int imageWidth = originalImage.getWidth(null),
		    imageHeight = originalImage.getHeight(null);
	    boolean wide = (double)imageWidth / imageHeight > (double)labelWidth / labelHeight;
	    double scale_factor = ((wide) ? ((double)labelWidth / imageWidth) : ((double)labelHeight / imageHeight));

	    double scaledW = imageWidth * scale_factor, scaledH = imageHeight * scale_factor;
	    g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
	    g2.drawImage(originalImage, 0, 0, (int)scaledW, (int)scaledH, null);
	}
	else
	{
	    super.paintComponent(g);
	}
    }

//    public Dimension getImageSize()
//    {
//	Dimension newImageSize = new Dimension(originalImage.getWidth(null), originalImage.getHeight(null));
//	Dimension displayDimensions = this.getSize();
//
//	double pageRatio = getImageRatio();
//	if (newImageSize.height >= newImageSize.width) //Portrait ratio page
//	{
//	    newImageSize.width = displayDimensions.width;
//	    newImageSize.height = (int)(displayDimensions.height / pageRatio);
//	}
//	else //Landscape ratio page
//	{
//	    newImageSize.height = displayDimensions.height;
//	    newImageSize.width = (int)(displayDimensions.width / pageRatio);
//	}
//
//	return newImageSize;
//    }
//
//    public double getImageRatio()
//    {
//	if (originalImage.getHeight(null) >= originalImage.getWidth(null))
//	{
//	    return (((double)originalImage.getWidth(null)) / ((double)originalImage.getHeight(null)));
//	}
//	else
//	{
//	    return (((double)originalImage.getHeight(null)) / ((double)originalImage.getWidth(null)));
//	}
//    }
}
